package action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Employee;
import biz.EmployeeBiz;
import biz.impl.EmployeeBizImpl;

public class LoginAction extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String page = "";
		try {
			page = validUser(request, response);
			System.out.println("page:"+page);
			RequestDispatcher rd = request.getRequestDispatcher(page);
			rd.forward(request, response);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
	}

	public String validUser(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		HttpSession session = request.getSession(false);
		if (session == null) {
			return "/login.jsp";
		} else {
			request.setCharacterEncoding("utf-8");
			String userName = request.getParameter("username");
			String password = request.getParameter("password");
			EmployeeBiz empBiz = new EmployeeBizImpl();
			Employee emp = empBiz.login(userName, password);
			if (emp == null) {
				return "/login.jsp";
			}
			return "index.jsp";
		}
	}
}
